package com.vehicle.controller;


import com.vehicle.form.AreaAddForm;
import com.vehicle.service.AreaService;
import com.vehicle.service.CategoryService;
import com.vehicle.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-21
 */
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    AreaService areaService;

    @Autowired
    CategoryService categoryService;
   //    添加
   @PostMapping("/addArea")
   public ResultVO addArea(HttpServletRequest request, @RequestBody AreaAddForm areaAddForm){
       String ip = request.getRemoteAddr();
       String id = request.getHeader("id");
       int categoryId = categoryService.getIdByName(areaAddForm.getCategoryName());
       return ResultVO.succ(areaService.addAreaMessage(ip,Integer.parseInt(id),areaAddForm,categoryId));
   }


    //查询所有停车区域信息
    @GetMapping("/areaList/{page}/{size}")
    public ResultVO getAreaList(@PathVariable("page") String page,@PathVariable("size") String size) {
        return ResultVO.succ(areaService.getAllAreaList(Integer.parseInt(page),Integer.parseInt(size)));
    }

    //根据区域位置或者停放车辆类型来进行模糊查询
    @GetMapping("/areaList/{content}")
    public ResultVO getAreaListByContent(@PathVariable("content") String content) {
        return ResultVO.succ(areaService.getAreaListByContent(content));
    }

    //获取所有的区域名称
    @GetMapping("/areaName")
    public ResultVO getAllAreaName(){
       return ResultVO.succ(areaService.getAreaNameList());
    }

    //根据name获得location

    @GetMapping("/location/{name}")
    public ResultVO getLocationByName(@PathVariable("name") String name){
       return ResultVO.succ(areaService.getLocationListByName(name));
    }

    //根据location和name获得categoryName
    @GetMapping("/categoryName/{name}/{location}")
    public ResultVO getLocationName(@PathVariable("name") String name,@PathVariable("location") String location){
        return ResultVO.succ(areaService.getCategoryNameListByNameAndLocation(name,location));
    }

    //获取areaId
    @GetMapping("/parkingId/{name}/{location}/{categoryName}")
    public ResultVO getParkingId(@PathVariable("name") String name,@PathVariable("location") String location,@PathVariable("categoryName") String categoryName){
        return ResultVO.succ(areaService.getParkingIdByAreaId(name,location,categoryName));
    }

    //删除停车区域信息
    @DeleteMapping("/{areaId}")
    public ResultVO deleteArea(HttpServletRequest request,@PathVariable("areaId") Integer areaId) {
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return ResultVO.succ(areaService.deleteById(ip,Integer.parseInt(id),areaId));
    }

    //修改停车区域信息
    @PutMapping("/")
    public ResultVO updateArea(HttpServletRequest request,AreaAddForm areaAddForm){
        String ip = request.getRemoteAddr();
        String aid = request.getHeader("id");
       Integer id = categoryService.getIdByName(areaAddForm.getCategoryName());
      boolean b =  areaService.updateAreaMessage(ip,Integer.parseInt(aid),areaAddForm,id);
      if(b){
          return ResultVO.succ(null);
      }
      return ResultVO.fail("更新失败");
    }

}

